package reu.model;

import java.util.LinkedList;
import java.util.Scanner;

public class SigList {
	public final int ROWS = 26;
	private LinkedList<Entry>[] entries;
	private final int LOWER_ROOT = 97;
	private final int UPPER_ROOT = 65;
	
	@SuppressWarnings("unchecked")
	public SigList(){
		entries = (LinkedList<Entry>[])new LinkedList[ROWS];
	}
	
	public int add(String fname, String mname, String lname){
		int entries_index = -1;
		int listSize = 0;
        char ch = lname.charAt(0);
        entries_index = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	entries_index = ch - LOWER_ROOT;
        }
		//instantiate the new entry
		Entry insert = new Entry(fname,mname,lname);
		listSize = entries[entries_index].size();
		if(listSize == 0){
			//add to front of list
			entries[entries_index].add(insert);
			entries_index = 0;
		}
		else{
			//if can't find word
			if(find(insert,entries_index) < 0 ){
				//get the size of the row
				int rSize = entries[entries_index].size();
				//if the list has entries, add the new entry to the first
				//index where the new entry is less than
				for(int i = 0; i < rSize; i++){
					if((entries[entries_index].get(i)).compare(insert) > 0 )
					{
						entries[entries_index].add(i, insert);
						entries_index = i;
						break;
					}
				}
				if(entries_index < 0){
					entries[entries_index].add(insert);
					entries_index = rSize;
				}
			}
			//else it returns a negative value for the entry was in the list
		}
		return entries_index;
	}
	
	public boolean remove(String fname, String mname, String lname){
		int result = -1;
        char ch = lname.charAt(0);
        int lRow = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	lRow = ch - LOWER_ROOT;
        }
		Entry old = new Entry(fname,mname,lname);
		result = find(old, lRow);
		if(result >= 0){
	        entries[lRow].remove(result);
			return true;
		}
		return false;
		
	}
	
	public boolean edit(String fname, String mname, String lname){
        Entry temporary = new Entry(fname, mname, lname);
        //Determine what row to reference in the list
        char ch = lname.charAt(0);
        int entries_index = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	entries_index = ch - LOWER_ROOT;
        }
        
        int tempReturnVal = find(temporary,entries_index);
        Entry entryToEdit = entries[entries_index].get(tempReturnVal);
        Scanner userInput = new Scanner(System.in);
        System.out.print("What field would you like to edit Name (N) or Infor (I)?");
        String fieldToEdit = userInput.nextLine();
        if(fieldToEdit.charAt(0) == 'N' || fieldToEdit.charAt(0) == 'n' ){
        	System.out.print("Which name woulf you like to edit First (F), Middle (M), or Last (L)");
        	String nameToEdit = userInput.nextLine();
        	if(nameToEdit.charAt(0) == 'F'|| nameToEdit.charAt(0) == 'f' ){
        		
        	}
        	if(nameToEdit.charAt(0) == 'M'|| nameToEdit.charAt(0) == 'm' ){
        		
        	}
        	if(nameToEdit.charAt(0) == 'L'|| nameToEdit.charAt(0) == 'l' ){
        		
        	}
        }
        
        if(fieldToEdit.charAt(0) == 'I' || fieldToEdit.charAt(0) == 'i'){
        	System.out.print("Which info would you like to edit First (F), Middle (M), or Last (L)");
        	String nameToEdit = userInput.nextLine();
        }
        
        
	
	}
	
	public int find(Entry e, int row){
		int result = -1;
		int rSize = entries[row].size();
		//if the list has entries
		for(int i = 0; i < rSize; i++){
			if((entries[row].get(i)).compare(e) == 0 )
			{
				 result = i;
				 break;
			}
		}
		return result;
	}

}
